package com.example.app2024.tab;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainer;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.app2024.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class tabActivity extends AppCompatActivity
{
      TabLayout tabLayout;
      ViewPager viewPager2;
      List<Fragment>  fragments;
     String[] titls = {"1左" , "2中" , "3右"};

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_tab);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            tabLayout = findViewById(R.id.tablayout01);
            viewPager2 = findViewById(R.id.viewpage01);
            fragments = new ArrayList<>();
            fragments.add(new Blank01Fragment());
            fragments.add(new Blank02Fragment());
            fragments.add(new Blank03Fragment());

            Adapter aa = new Adapter( getSupportFragmentManager(),  fragments);
            viewPager2.setAdapter(aa);

            tabLayout.setupWithViewPager(viewPager2);
      }

      private  class  Adapter extends FragmentPagerAdapter
      {
            private  List<Fragment> list;

            public Adapter(@NonNull FragmentManager fm , List<Fragment> list)
            {
                  super(fm);
                  this.list = list;
            }

            @NonNull
            @Override
            public Fragment getItem(int position)
            {
                  return list.get(position);
            }

            @Override
            public int getCount()
            {
                  return list.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position)
            {
                  return titls[position];
            }
      }
}